package com.cloud.DrugManagement.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.DrugManagement.enums.OrderState;
import com.cloud.DrugManagement.pojo.DrugPrice;
import com.cloud.DrugManagement.pojo.OrderDetails;
import com.cloud.DrugManagement.pojo.OrderContent;
import com.cloud.DrugManagement.pojo.Supplier;
import com.cloud.DrugManagement.service.*;
import com.cloud.DrugManagement.pojo.vo.OrderDetailsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhozixiong
 * @date 2023/02/11
 */
@Slf4j
@Api(tags = "订单")
@CrossOrigin
@RestController
@RequiredArgsConstructor
public class OrderController {

    private final OrderDetailsService orderDetailsService;
    private final OrderContentService orderContentService;
    private final ReceiptMonocularService receiptMonocularService;
    private final SupplierService supplierService;
    private final DrugPriceService drugPriceService;

    @ApiOperation(value = "提交订单")
    @GetMapping("/submitOrder/{oid}")
    public Object submitOrder(@PathVariable Long oid) {
        System.out.println(oid);
        //判断订单状态是否能修改
        if(judgeOrderState(oid)) {
            //记录药品当前的批发价格
            orderContentService.updateOrderPrice(oid);
            //修改订单状态
            orderDetailsService.updateCreationTimeStateByOid(oid);
            return "ok";
        }else {
            return "订单无法修改";
        }
    }

    @ApiOperation(value = "获取订单单头详细信息")
    @GetMapping("/getOrderDetail/{oid}")
    public Object getOrderDetail(@PathVariable Long oid) {
        OrderDetails order = orderDetailsService.searchAllByOid(oid);
        OrderDetailsVo orderDetailsVo;
        if(OrderState.DELIVERED.equals(order.getState()) || OrderState.COMPLETE.equals(order.getState())) {
            //查询订单的总金额
            double totalAmount = receiptMonocularService.searchTotalAmount(oid);
            orderDetailsVo = new OrderDetailsVo(order, totalAmount);
        }else {
            orderDetailsVo = new OrderDetailsVo(order);
        }
        return orderDetailsVo;
    }

    @ApiOperation(value = "获取订单单头列表")
    @GetMapping({"/getOrderDetails/{pages}/{oid}", "/getOrderDetails/{pages}"})
    public Object getOrderDetails(@PathVariable int pages,@PathVariable(required = false) Long oid) {
        Page<OrderDetails> page = orderDetailsService.searchAllOrOid(pages, oid);
        return page;
    }

    @ApiOperation(value = "根据订单状态查询订单列表")
    @GetMapping("/getOrderDetail/{pages}/{state}")
    public Object getOrderDetails(@PathVariable int pages, @PathVariable() int state) {
        return "还没写";
    }

    @ApiOperation(value = "获取订单单目")
    @GetMapping("/getOrder/{pages}/{oid}")
    public Object getOrder(@PathVariable int pages , @PathVariable Long oid) {
        OrderDetails order = orderDetailsService.searchAllByOid(oid);
        //订单单目
        Page<OrderContent> page;
        //判断订单状态
        if(order.getState().equals(OrderState.NOT_SUBMITTED)) {
            //订单未提交
            page = orderContentService.notSubmitSearch(pages, oid);
        }else {
            //订单已提交
            page = orderContentService.submitSearch(pages, oid);
        }
        return page;
    }

    @ApiOperation(value = "供应商供应的商品")
    @GetMapping({"/getDrug/{pages}/{oid}/{parameter}", "/getDrug/{pages}/{oid}"})
    public Object getDrug(@PathVariable int pages, @PathVariable int oid, @PathVariable(required = false) Object parameter) {

        OrderDetails orderDetails = orderDetailsService.searchAllByOid(oid);
        Page<DrugPrice> page = drugPriceService.searchAllBySidOrDidOrDname(pages, orderDetails.getSid(), parameter);
        return page;
    }

    @ApiOperation(value = "创建订单")
    @PostMapping("/createOrder")
    public Object createOrder(@RequestBody JSONObject object) {
        int sid = object.getInteger("sid");
        Supplier supplier = supplierService.searchAllBySid(sid);
        OrderDetails orderDetails = new OrderDetails();
        orderDetails.setSid(sid);
        orderDetails.setSname(supplier.getSname());
        orderDetails.setState(OrderState.NOT_SUBMITTED);
        orderDetailsService.insert(orderDetails);
        Map<String, Object> map = new HashMap<>(1);
        map.put("oid", orderDetails.getOid());
        return map;
    }

    @ApiOperation(value = "添加订单单目")
    @PostMapping("/createOrderContent")
    public Object createOrderContent(@RequestBody OrderContent orderContent) {
        //先判断订单是否能修改
        if(judgeOrderState(orderContent.getOid())) {
            //写进数据库
            boolean sr = orderContentService.insert(orderContent);
            if(sr) {
                return "ok";
            }else {
                return "full";
            }
        }else {
            return "订单无法修改";
        }
    }

    @ApiOperation(value = "修改订单内容")
    @PostMapping("/upOrderContent")
    public Object updateOrderContent(@RequestBody OrderContent orderContent) {
        if(judgeOrderState(orderContent.getOid())) {
            boolean sr = orderContentService.updateQuantityByOidAndDid(orderContent.getOrderQuantity(), orderContent.getOid(), orderContent.getDid());
            if(sr) {
                return "ok";
            }else {
                return "full";
            }
        }else {
            return "订单无法修改";
        }
    }

    @ApiOperation(value = "删除订单")
    @GetMapping("/delOrder/{oid}")
    public Object delOrder(@PathVariable long oid) {
        if(judgeOrderState(oid)) {
            if(orderDetailsService.delByOid(oid)) {
                return "ok";
            }else {
                return "full";
            }
        }else {
            return "订单无法修改";
        }
    }

    @ApiOperation(value = "删除订单内容(单条)")
    @GetMapping("/dleOrderContent/{oid}/{sid}")
    public Object dleOrderContent(@PathVariable long oid, @PathVariable int sid) {
        if(judgeOrderState(oid)) {
            if(orderContentService.delByOidAndDid(oid, sid)) {
                return "ok";
            }else {
                return "full";
            }
        }else {
            return "订单无法修改";
        }
    }

    /**
     * 判断订单的状态
     */
    private boolean judgeOrderState(long oid) {
        OrderDetails orderDetails = orderDetailsService.searchAllByOid(oid);
        //未提交和提交阶段可以修改、删除订单和订单内容
        return orderDetails.getState().equals(OrderState.NOT_SUBMITTED);
    }
}
